package com.scott.itcast.thread.lesson05;

/**
 * 同步的例子
 */
public class TraditionalThreadSynchronized {
	
	private void init(){
		final Outputer outputer = new Outputer();
		
		//第1个线程
		new Thread(new Runnable(){
			public void run() {
				while(true){
					try {
						Thread.sleep(1000);
						outputer.output("801234567898");
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
			
		}).start();
		
		
		//第2个线程
		new Thread(new Runnable(){
			public void run() {
				while(true){
					try {
						Thread.sleep(1000);
						outputer.output("masdfghjklqwertyuiopm");
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
			
		}).start();
	}
	
	public static void main(String[] args) {
		new TraditionalThreadSynchronized().init();
	}
	
	
	class Outputer{
		
		//同步整个方法
		public synchronized void output(String name){
			int len = name.length();
			for(int i=0;i<len;i++){
				System.out.print(name.charAt(i));
			}
			//换行
			System.out.println();
			
		}
	}

}



